package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 权限管理permission
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    //    从远程得到信息，生成代理对象
    @Reference
    private PermissionService permissionService;

    @PreAuthorize("hasAnyAuthority('CHECKITEM_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission) {
    //     1.添加权限
        try {
            permissionService.add(permission);
            return new Result(true, "添加成功！！！");
        } catch (Exception e) {
            return new Result(false, "添加失败！！！");
        }
    }

    /**
     * 编辑页面回显
     * "/permission/findPermissionByid.do?id="+row.id
     */
    @RequestMapping("/findPermissionByid")
    public Result findPermissionByid(Integer id){
        try {
            Permission permission =  permissionService.findPermissionByid(id);
            return new  Result(true,"数据回显成功！",permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "数据回显失败！");
        }
    }
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    /**
     * 分页--检查项目
     * 请求地址：/permission/findPage.do
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = permissionService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;
    }
    /**
     * 编辑权限页面
     * /permission/edit.do",data
     */
    @PreAuthorize("hasAnyAuthority('CHECKITEM_EDIT')")
    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try {
            permissionService.update(permission);
            return new  Result(true,"更新权限成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            return new  Result(false,"更新权限失败！！！");
        }
    }
    @PreAuthorize("hasAnyAuthority('CHECKITEM_DELETE')")
    /**
     * 删除检测项目
     * "/permission/delete.do?id="+row.id
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            permissionService.delete(id);
            return new Result(true, "注意已删除权限!!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败！！！");
        }
    }
    /**
     *  查找全部权限项目
     *  /permission/finAllPermission.do
     */
    @RequestMapping("/finAllPermission")
    public Result finAllPermission(){
        try{
    //      成功返回数据并且 --返回标志位flag-true
            List<Permission> permission=permissionService.finAllPermission();
            return new Result(true,   MessageConstant.QUERY_CHECKITEM_SUCCESS,permission);
        }catch (Exception e){
            return  new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

}
